/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.restaurant.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.common.tool.CommonUtil;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.restaurant.entity.ExpensesBudget;
import org.springblade.modules.restaurant.enums.StatusEnums;
import org.springblade.modules.restaurant.mapper.ExpensesBudgetMapper;
import org.springblade.modules.restaurant.service.IExpensesBudgetService;
import org.springblade.modules.restaurant.vo.ExpensesBudgetVO;
import org.springblade.modules.system.service.ITenantService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 预算支出表 服务实现类
 *
 * @author Blade
 * @since 2025-03-03
 */
@Service
@AllArgsConstructor
public class ExpensesBudgetServiceImpl extends BaseServiceImpl<ExpensesBudgetMapper, ExpensesBudget> implements IExpensesBudgetService {
	private ITenantService tenantService;

	@Override
	public IPage<ExpensesBudgetVO> selectExpensesBudgetPage(IPage<ExpensesBudgetVO> page, ExpensesBudgetVO expensesBudget) {
		return page.setRecords(baseMapper.selectExpensesBudgetPage(page, expensesBudget));
	}

	@Override
	public BigDecimal getExpensesBudgetDayAvg(String time, String tenantId) {
		// 时间转换为 yyyy-MM
		String timeYm = DateUtil.format(DateUtil.parse(time), DatePattern.NORM_MONTH_PATTERN);

		List<ExpensesBudget> expensesBudgetList = lambdaQuery()
			.eq(ExpensesBudget::getBusinessTime, timeYm)
			.eq(ExpensesBudget::getTenantId, tenantId)
			.eq(BaseEntity::getStatus, StatusEnums.ENABLE.getStatus())
			.list();

		return expensesBudgetList.stream().map(ExpensesBudget::getDailyExpendAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public BigDecimal gen(String month, String previousMonth) {
		List<String> tenantIds = tenantService.getAllTenantIds();
		for (String tenantId : tenantIds) {
			List<ExpensesBudget> expensesBudgetList = lambdaQuery()
				.eq(ExpensesBudget::getBusinessTime, previousMonth)
				.eq(ExpensesBudget::getTenantId, tenantId)
				.eq(BaseEntity::getStatus, StatusEnums.ENABLE.getStatus())
				.list();
			List<ExpensesBudget> monthList = new ArrayList<>();

			// 删除当月数据
			remove(Wrappers.<ExpensesBudget>lambdaQuery().eq(ExpensesBudget::getBusinessTime, month).eq(ExpensesBudget::getTenantId, tenantId));
			// 获取当月天数
			int days = CommonUtil.getDays(month, DatePattern.NORM_MONTH_PATTERN);
			for (ExpensesBudget expensesBudget : expensesBudgetList) {
				ExpensesBudget budget = new ExpensesBudget();
				budget.setTenantId(tenantId);
				budget.setBusinessTime(month);
				budget.setClassification(expensesBudget.getClassification());
				budget.setClassificationText(expensesBudget.getClassificationText());
				budget.setDays(days);
				BigDecimal dailyExpendAmount = expensesBudget.getMonthlyExpendAmount().divide(new BigDecimal(days), 2, RoundingMode.HALF_UP);
				budget.setMonthlyExpendAmount(expensesBudget.getMonthlyExpendAmount());
				budget.setDailyExpendAmount(dailyExpendAmount);
				budget.setRemark(expensesBudget.getRemark());
				budget.setEnclosure(expensesBudget.getEnclosure());
				monthList.add(budget);
			}
			saveBatch(monthList);
		}
		return null;
	}
}
